/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.comut.crud.bo;

import com.comut.crud.dao.MobileDao;
import com.comut.crud.ejb.interfaces.IMobileAction;
import com.comut.crud.pojo.mobile.Mobile;
import com.comut.crud.pojo.mobile.MobileUpdate;
import com.comut.crud.util.ParseJson;
import com.comut.lib.error.ErrorCodes;
import com.comut.lib.exception.JSONMalFormedException;
import com.comut.lib.exception.ObjectFoundException;
import com.comut.lib.exception.ObjectNotFoundException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.apache.log4j.Logger;

/**
 *
 * @author asantos
 */
public class MobileHandler implements IMobileAction {
    
    private static Logger _logger = Logger.getLogger(MobileHandler.class.getName());
    private Connection _moovefleetConnection;
    private Connection _ifleetConnection;
    private MobileDao mobileDao;

    public MobileHandler(Connection moovefleetConnection, Connection ifleetConnection) {
        this._moovefleetConnection = moovefleetConnection;
        this._ifleetConnection = ifleetConnection;
        mobileDao = new MobileDao(_moovefleetConnection, _ifleetConnection);
    }

    public Mobile insertMobile(Mobile mobile) throws ObjectFoundException {
        if(mobile.isValid()){
            mobileDao.checkUniquenessOnCreate(mobile.getId_fleet(), mobile.getId_mobile(), mobile.getLicense_plate(), mobile.getSerial_number(), mobile.getImei(), mobile.getGsm_number(), mobile.getIccid());
            _logger.info("checkUniquenessOnCreate done");
            int uid = mobileDao.createMobile(mobile);
            Mobile toRet = new Mobile();
            toRet.setUid(uid);
            _logger.info(toRet.toString());
            return toRet;
        }
        else
            throw new JSONMalFormedException("The mandatory attributes are not in json.", ErrorCodes.JSON_MAL_FORMED);
    }

    public void deleteMobile(Mobile mobile) throws ObjectNotFoundException {
        _logger.info(mobile.toString());      
        mobileDao.deleteMobile(mobile.getUid());
    }

    public void updateMobile(String mobile) throws ObjectNotFoundException {
        _logger.info(mobile.toString());
        //check if the BasicDBObject is well formed
        MobileUpdate mu = ParseJson.getInstance().getMobileUpdateValidated(mobile);
        Mobile aux = new Mobile();
        aux.setUid(mu.getUid());
        aux = getMobile(aux);
        mu.setId_fleet(aux.getId_fleet());
        mu.setId_mobile(aux.getId_mobile());
        
        if(mu.isLicense_plateFlag()){
            mobileDao.checkUniqueLicensePlate(mu.getId_fleet(), mu.getId_mobile(), mu.getLicense_plate());
        }
        
        if(mu.isSerial_numberFlag()){
            mobileDao.checkUniqueSerialNumber(mu.getId_fleet(), mu.getId_mobile(), mu.getSerial_number());
        }
        
        if(mu.isImeiFlag()){
            mobileDao.checkUniqueImei(mu.getId_fleet(), mu.getId_mobile(), mu.getImei());
        }
        
        if(mu.isGsm_numberFlag()){
            mobileDao.checkUniqueGsmNumber(mu.getId_fleet(), mu.getId_mobile(), mu.getGsm_number());
        }
        
        if(mu.isIccidFlag()){
            mobileDao.checkUniqueICCID(mu.getId_fleet(), mu.getId_mobile(), mu.getIccid());
        }
        
        //update the mobile
        mobileDao.updateMobile(mu);
    }

    public Mobile getMobile(Mobile mobile) throws ObjectNotFoundException {
        
        ResultSet rs = mobileDao.getMobile(mobile.getUid());
        Mobile toRet = new Mobile();
        try {
            if (rs.next()) {
                toRet.setId_fleet(rs.getInt(1));
                toRet.setId_mobile(rs.getInt(2));
            }
            else{
                throw new ObjectFoundException("The UID: "+mobile.getUid()+" was not found", ErrorCodes.OBJECT_NOT_FOUND);
            }
                
        } catch (SQLException ex) {
            _logger.error(ex);
        }

        _logger.info(toRet.toString());
        return toRet;
    }
    
}
